﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using ArkanoidX.Objects;

namespace ArkanoidX.UI
{
    public class Text : Window
    {
        public String Value
        {
            get { return _value; }
            set 
            {
                _value = value;
                UpdateTextSize();
            }
        }

        public SpriteFont Font
        {
            get { return _font; }
            set
            {
                _font = value;
                UpdateTextSize();
            }
        }

        public Vector2 Position;

        public Color Color = Color.White;

        protected Vector2 _scale = new Vector2(1, 1);
        protected String _value = "";
        protected SpriteFont _font;

        public Vector2 BaseSize;
        public Vector2 BaseHalfSize;

        public Vector2 Scale
        {
            get { return _scale; }
            set 
            { 
                _scale = value;
            }
        }

        public Rectangle Rectangle
        {
            get
            {
                Vector2 topLeft = this.Position - Size * 0.5f;

                return new Rectangle((int)topLeft.X, (int)topLeft.Y, (int)Size.X, (int)Size.Y);
            }
        }

        public Vector2 Size
        {
            get
            {
                return this.BaseSize * this.Scale;
            }
        }

        public Text(MainGame game)
            : base(game)
        {
            _font = game.Content.Load<SpriteFont>("Levels");    // default font
            canBeActivated = false;
        }

        public void UpdateTextSize()
        {
            BaseSize = GetTextSize();
            BaseHalfSize = BaseSize * 0.5f;
        }

        public Vector2 GetTextSize()
        {
            return Font.MeasureString(Value);
        }

        public Vector2 CalcAlign(Rectangle rect, AlignType type, float xMargin, float yMargin)
        {
            UpdateTextSize();
            Vector2 size = this.BaseSize * this.Scale;
            return CalcAlign(rect, type, size, xMargin, yMargin);
        }

        public void Align(Rectangle rect, AlignType type, float xMargin, float yMargin)
        {
            Vector2 pos = CalcAlign(rect, type, xMargin, yMargin);
            Position = pos;
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            sBatch.DrawString(Font, Value, Position, Color, 0, BaseHalfSize, _scale
                , SpriteEffects.None, 0);

            base.Draw(gameTime);
        }
    }
}
